#include "MQTTPublisher.h"
#include <cstring>

MQTTPublisher::MQTTPublisher(const char *host, const char *client_id,
                             const char *topic, int qos)
    : BaseMQTTClient(client_id, host), default_topic(topic), default_qos(qos) {}

void MQTTPublisher::publish(const std::string &message) {
  int ret =
      mosquitto_publish(mosq, nullptr, default_topic.c_str(), message.size(),
                        message.c_str(), default_qos, false);
  if (ret == MOSQ_ERR_SUCCESS) {
    std::cout << "📤 发布消息: " << message << " -> 主题: " << default_topic
              << std::endl;
  } else {
    std::cerr << "❌ 发布失败: " << mosquitto_strerror(ret) << std::endl;
  }
}
